﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig.Controls
{
    public partial class Flowmeter : UserControl
    {
        public int intIndex;
        public Section section;
        public int intFss;
        public int intDms;

        public Flowmeter()
        {
            InitializeComponent();
        }

        protected override void CreateHandle()
        {
            if (!IsHandleCreated)
            {
                try
                {
                    base.CreateHandle();
                }
                catch { }
                finally
                {
                    if (!IsHandleCreated)
                    {
                        base.RecreateHandle();
                    }
                }
            }
        }

        public void ControlsDispose()
        {
            foreach (Control item in pnlSet.Controls)
            {
                item.Dispose();
            }
        }

        public void InitShow(int intFs, int intDm)
        {
            ControlsDispose();
            this.pnlSet.Controls.Clear();
            intFss = intFs;
            intDms = intDm;
            InitImage(intFs, intDm);
            section = new Section(intIndex);
            section.Location = new Point(35, 0);
            section.InitShow(intFs, intDm);
            this.pnlSet.Controls.Add(section);
            pnlSet.Refresh();
        }

        public void InitValue(string strText, int index, int intFs, int intDm)
        {
            ControlsDispose();
            this.pnlSet.Controls.Clear();
            gc.Text = strText;
            intIndex = index;
            intFss = intFs;
            intDms = intDm;
            InitImage(intFs, intDm);
            section = new Section(index);
            section.Location = new Point(35, 0);
            section.InitShow(intFs, intDm);
            this.pnlSet.Controls.Add(section);
            pnlSet.Refresh();
        }

        /// <summary>
        /// 初始断面选择图片
        /// </summary>
        /// <param name="intFs">流量计算方式</param>
        /// <param name="intDm">流量槽断面选择</param>
        public void InitImage(int intFs, int intDm)
        {
            if (intFs == 0)
            {
                if (intDm == 0)
                {
                    pb.Image = global::RTUParameterConfig.Properties.Resources._1;
                }

                if (intDm == 1)
                {
                    pb.Image = global::RTUParameterConfig.Properties.Resources._8;
                }

                if (intDm == 2)
                {
                    pb.Image = global::RTUParameterConfig.Properties.Resources._9;
                }

                if (intDm == 3)
                {
                    pb.Image = global::RTUParameterConfig.Properties.Resources._10;
                }

                if (intDm == 4)
                {
                    pb.Image = global::RTUParameterConfig.Properties.Resources._11;
                }

                if (intDm == 5)
                {
                    pb.Image = global::RTUParameterConfig.Properties.Resources._12;
                }
            }

            if (intFs == 1 || intFs == 3 || intFs == 4)
            {
                if (intDm == 0)
                {
                    pb.Image = global::RTUParameterConfig.Properties.Resources._3;
                }

                if (intDm == 1)
                {
                    pb.Image = global::RTUParameterConfig.Properties.Resources._2;
                }

                if (intDm == 2)
                {
                    pb.Image = global::RTUParameterConfig.Properties.Resources._4;
                }

                if (intDm == 3)
                {
                    pb.Image = global::RTUParameterConfig.Properties.Resources._5;
                }

                if (intDm == 4)
                {
                    pb.Image = global::RTUParameterConfig.Properties.Resources._6;
                }

                if (intDm == 5)
                {
                    pb.Image = null;
                }

                if (intDm == 6)
                {
                    pb.Image = null;
                }
            }

            if (intFs == 2)
            {
                if (intDm == 0)
                {
                    pb.Image = global::RTUParameterConfig.Properties.Resources._7;
                }
            }
        }

        /// <summary>
        /// 获取流量积算配置
        /// </summary>
        public void GetValue()
        {
            if (intIndex == 0)
            {
                ParameterConfig.rtuConfigInfo.WaterFlowCount_Enable |= 0x0001 << 0;
            }

            if (intIndex == 1)
            {
                ParameterConfig.rtuConfigInfo.WaterFlowCount_Enable |= 0x0001 << 1;
            }
            ParameterConfig.rtuConfigInfo.WaterFlowCountSelect[intIndex] = Convert.ToByte(intFss);
            ParameterConfig.rtuConfigInfo.WaterFlowSectionSelect[intIndex] = Convert.ToByte(intDms);
        }

        private void btnOther_Click(object sender, EventArgs e)
        {
            try
            {
                FlowmeterSetting fs = new FlowmeterSetting(intIndex);
                fs.StartPosition = FormStartPosition.CenterParent;
                fs.ShowDialog();
            }
            catch
            {
                Common.ShowErrorMessage("加载高级设置失败！");
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除流量积算?", "删除流量积算", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                Control c = this.Parent;
                c.Controls.Remove(this);
                if (intIndex == 0)
                {
                    //ParameterConfig.f1 = null;
                }

                if (intIndex == 0)
                {
                    //ParameterConfig.f2 = null;
                }
            }
        }
    }
}
